package com.kk.bird;

/**
 * @author kzy
 * @version 1.0
 * @date 2023/2/7
 */

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Bird {
    BufferedImage image = ImageIO.read(this.getClass().getResource(GameConfig.pictureUrl+"0.png"));
    int x;
    int y;
    int width;
    int height;
    int size;
    double g;
    double t;
    double v0;
    double speed;
    double s;
    double alpha;
    BufferedImage[] images;
    int index;

    public Bird() throws Exception {
        this.width = this.image.getWidth();
        this.height = this.image.getHeight();
        this.x = 132;
        this.y = 280;
        this.size = 40;
        this.g = 4.0D;
        this.v0 = 20.0D;
        this.t = 0.25D;
        this.speed = this.v0;
        this.s = 0.0D;
        this.alpha = 0.0D;
        this.images = new BufferedImage[8];

        for(int i = 0; i < 8; ++i) {
            this.images[i] = ImageIO.read(this.getClass().getResource(GameConfig.pictureUrl+ i + ".png"));
        }

        this.index = 0;
    }

    public void fly() {
        ++this.index;
        this.image = this.images[this.index / 12 % 8];
    }

    public void step() {
        double v0 = this.speed;
        this.s = v0 * this.t + this.g * this.t * this.t / 2.0D;
        this.y -= (int)this.s;
        double v = v0 - this.g * this.t;
        this.speed = v;
        this.alpha = Math.atan(this.s / 8.0D);
    }

    public void flappy() {
        this.speed = this.v0;
    }

    public boolean hit(Ground ground) {
        boolean hit = this.y + this.size / 2 > ground.y;
        if (hit) {
            this.y = ground.y - this.size / 2;
            this.alpha = -1.5707963267948966D;
        }

        return hit;
    }

    public boolean hit(Column column) {
        if (this.x > column.x - column.width / 2 - this.size / 2 && this.x < column.x + column.width / 2 + this.size / 2) {
            return this.y <= column.y - column.gap / 2 - this.size / 2 || this.y >= column.y + column.gap / 2 - this.size / 2;
        } else {
            return false;
        }
    }
}
